package com.xintonghua.bussiness.ui.user;

import android.graphics.Paint;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.api.manager.AuthManager;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.Auth;
import com.xintonghua.bussiness.ui.MainActivity;
import com.xintonghua.bussiness.util.MyState;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {
    @BindView(R.id.tv_username)
    EditText tvUsername;
    @BindView(R.id.tv_password)
    EditText tvPassword;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.tv_forget)
    TextView tvForget;
    @BindView(R.id.tv_user_agreement)
    TextView tvUserAgreement;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initImmersionBar();
        mImmersionBar.statusBarColor(R.color.red);
//        getSupportActionBar().getCustomView().findViewById(android.R.id.home).setVisibility(View.GONE);
        userAgreement();
        userForgetPassWord();

    }

    /**
     * 忘记密码
     */
    private void userForgetPassWord() {
        tvForget.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
    }

    /**
     * 用户协议
     */
    private void userAgreement() {
        SpannableString userAgreement = new SpannableString("点击登录，即表示同意《用户协议》");
        userAgreement.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.white)), userAgreement.length() - 6, userAgreement.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tvUserAgreement.setText(userAgreement);
    }

    @OnClick({R.id.tv_password, R.id.btn_login, R.id.tv_user_agreement})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_password:
                break;
            case R.id.btn_login:
                httpCent.login(getStr(tvUsername), getStr(tvPassword), this, MyState.requestOne);
                break;
            case R.id.tv_user_agreement:

                break;
        }
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                Auth auth = JSONObject.parseObject((String) data, Auth.class);
                AuthManager.cacheAuth(this, auth);
                openActivity(MainActivity.class);
                break;
        }
    }
}
